package views.cad;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.awt.event.MouseListener;
import java.sql.Date;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.text.MaskFormatter;

import models.cad.Horario;
import models.cad.Usuario;
import supers.GUIInternalFrame;
import supers.GUICadInterface;
import utils.GlobalDeclarations;
import views.SisDskGUI;

@SuppressWarnings("serial")
public class CadHorGUI extends GUIInternalFrame implements GUICadInterface{
	public JTextField tfNome;
	public JFormattedTextField tfCod;
	public JButton btNovo;
	public JButton btExcluir;
	public JButton btListar;
	public JRadioButton rbManha;
	public JRadioButton rbTarde;
	public JRadioButton rbNoite;
	public JFormattedTextField tfInicio;
	public JFormattedTextField tfTermino;
	public JCheckBox cbSex;
	public JCheckBox cbDom;
	public JCheckBox cbSab;
	public JCheckBox cbSeg;
	public JCheckBox cbTer;
	public JCheckBox cbQua;
	public JCheckBox cbQui;
	
	public CadHorGUI() {
		setTitle("Cadastro de Hor\u00E1rios");
		setSize(new Dimension(325, 285));
		
		tfNome = new JTextField();
		tfNome.setBounds(71, 56, 224, 20);
		getContentPane().add(tfNome);
		tfNome.setColumns(10);
		try {
			tfCod = new JFormattedTextField(new MaskFormatter("###"));
			tfCod.setColumns(3);
			tfCod.setBounds(71, 25, 30, 20);
			getContentPane().add(tfCod);
			
			JLabel lblNewLabel = new JLabel("Cod.:");
			lblNewLabel.setLabelFor(tfCod);
			lblNewLabel.setBounds(27, 28, 34, 14);
			getContentPane().add(lblNewLabel);
			
			JLabel lblNewLabel_1 = new JLabel("Nome:");
			lblNewLabel_1.setLabelFor(tfNome);
			lblNewLabel_1.setBounds(27, 59, 46, 14);
			getContentPane().add(lblNewLabel_1);
			
			JPanel panel_1 = new JPanel();
			panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			panel_1.setBounds(27, 207, 268, 26);
			getContentPane().add(panel_1);
			panel_1.setLayout(null);
			
			btNovo = new JButton("Novo");
			ImageIcon imageIcon = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtNovo));
			Image img = imageIcon.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btNovo.setIcon(new ImageIcon(img));
			btNovo.setBounds(1, 1, 80, 23);
			panel_1.add(btNovo);
			
			btExcluir = new JButton("Excluir");
			btExcluir.setEnabled(false);
			ImageIcon imageIcon2 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtExcluir));
			Image img2 = imageIcon2.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btExcluir.setIcon(new ImageIcon(img2));
			btExcluir.setBounds(91, 1, 85, 23);
			panel_1.add(btExcluir);
			
			btListar = new JButton("Listar");
			ImageIcon imageIcon3 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtListar));
			Image img3 = imageIcon3.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btListar.setIcon(new ImageIcon(img3));
			btListar.setBounds(186, 1, 80, 23);
			panel_1.add(btListar);
			
			rbManha = new JRadioButton("Manh\u00E3");
			rbManha.setSelected(true);
			rbManha.setBounds(64, 83, 57, 23);
			getContentPane().add(rbManha);
			
			rbTarde = new JRadioButton("Tarde");
			rbTarde.setBounds(123, 83, 57, 23);
			getContentPane().add(rbTarde);
			
			rbNoite = new JRadioButton("Noite");
			rbNoite.setBounds(182, 83, 57, 23);
			getContentPane().add(rbNoite);
			
			JLabel lblTurno = new JLabel("Turno:");
			lblTurno.setBounds(27, 87, 46, 14);
			getContentPane().add(lblTurno);
			
			JLabel lblIncio = new JLabel("In\u00EDcio:");
			lblIncio.setBounds(27, 172, 34, 14);
			getContentPane().add(lblIncio);
			
			tfInicio = new JFormattedTextField(new MaskFormatter("##:##"));
			tfInicio.setBounds(71, 169, 34, 20);
			getContentPane().add(tfInicio);
			
			tfTermino = new JFormattedTextField(new MaskFormatter("##:##"));
			tfTermino.setBounds(176, 169, 34, 20);
			getContentPane().add(tfTermino);
			
			JLabel lblTrmino = new JLabel("T\u00E9rmino:");
			lblTrmino.setBounds(123, 172, 46, 14);
			getContentPane().add(lblTrmino);
			
			JLabel lblDias = new JLabel("Dias:");
			lblDias.setBounds(27, 117, 34, 14);
			getContentPane().add(lblDias);
			
			cbSeg = new JCheckBox("Seg");
			cbSeg.setSelected(true);
			cbSeg.setBounds(66, 113, 43, 23);
			getContentPane().add(cbSeg);
			
			cbTer = new JCheckBox("Ter");
			cbTer.setBounds(111, 113, 43, 23);
			getContentPane().add(cbTer);
			
			cbQua = new JCheckBox("Qua");
			cbQua.setBounds(156, 113, 45, 23);
			getContentPane().add(cbQua);
			
			cbQui = new JCheckBox("Qui");
			cbQui.setBounds(201, 113, 43, 23);
			getContentPane().add(cbQui);
			
			cbSab = new JCheckBox("Sab");
			cbSab.setBounds(66, 139, 43, 23);
			getContentPane().add(cbSab);
			
			cbSex = new JCheckBox("Sex");
			cbSex.setBounds(246, 113, 43, 23);
			getContentPane().add(cbSex);
			
			cbDom = new JCheckBox("Dom");
			cbDom.setBounds(111, 139, 46, 23);
			getContentPane().add(cbDom);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setListener(ActionListener ac, FocusListener fl, MouseListener ml) {
        tfCod.addFocusListener(fl);
        tfTermino.addFocusListener(fl);
        btNovo.addActionListener(ac);
        btExcluir.addActionListener(ac);
        btListar.addActionListener(ac);
        rbManha.addActionListener(ac);
        rbTarde.addActionListener(ac);
        rbNoite.addActionListener(ac);
	}

	@Override
	public void limpar(){
		tfCod.setText("");
		tfNome.setText("");
		tfInicio.setText("");
		tfTermino.setText("");
		rbManha.setSelected(true);
		rbTarde.setSelected(false);
		rbNoite.setSelected(false);
		cbSeg.setSelected(true);	//S
		cbTer.setSelected(false);	//T
		cbQua.setSelected(false);	//Q
		cbQui.setSelected(false);	//U
		cbSex.setSelected(false);	//E
		cbSab.setSelected(false);	//A
		cbDom.setSelected(false);	//D
		btExcluir.setEnabled(false);
	}

	public void setDados(Object obj){
		Horario hor = (Horario) obj;
		tfCod.setText(hor.getCodigo());
		tfNome.setText(hor.getNome());
		tfInicio.setText(new SimpleDateFormat("hh:mm").format(hor.getDtIni()));
		tfTermino.setText(new SimpleDateFormat("hh:mm").format(hor.getDtFin()));
		rbManha.setSelected(false);
		rbTarde.setSelected(false);
		rbNoite.setSelected(false);
		switch(hor.getTurno()){
			case 'M':{
				rbManha.setSelected(true);
				break;
			}
			case 'T':{
				rbTarde.setSelected(true);
				break;
			}
			case 'N':{
				rbNoite.setSelected(true);
				break;
			}
		}
		cbSeg.setSelected(false);	//S
		cbTer.setSelected(false);	//T
		cbQua.setSelected(false);	//Q
		cbQui.setSelected(false);	//U
		cbSex.setSelected(false);	//E
		cbSab.setSelected(false);	//A
		cbDom.setSelected(false);	//D
		char Dias[] = hor.getDias().toCharArray();
		for (char c : Dias)
			switch(c){
				case 'S':{
					cbSeg.setSelected(true);
					break;
				}
				case 'T':{
					cbTer.setSelected(true);
					break;
				}
				case 'Q':{
					cbQua.setSelected(true);
					break;
				}
				case 'U':{
					cbQui.setSelected(true);
					break;
				}
				case 'E':{
					cbSex.setSelected(true);
					break;
				}
				case 'A':{
					cbSab.setSelected(true);
					break;
				}
				case 'D':{
					cbDom.setSelected(true);
					break;
				}
			}
	}
	
	@SuppressWarnings("deprecation")
	public Horario getDados(){
		Horario hor = new Horario();
		hor.setCodigo(tfCod.getText());
		hor.setNome(tfNome.getText());
		String[] hm;
		hm = tfInicio.getText().split(":");
		hor.setDtIni(new Time(Integer.valueOf(hm[0]), Integer.valueOf(hm[1]), 0));
		hm = tfTermino.getText().split(":");
		hor.setDtFin(new Time(Integer.valueOf(hm[0]), Integer.valueOf(hm[1]), 0));
		if(rbManha.isSelected())	hor.setTurno('M');
		if(rbTarde.isSelected())	hor.setTurno('T');
		if(rbNoite.isSelected())	hor.setTurno('N');
		String dias = "";
		if(cbSeg.isSelected())		dias += "S";
		if(cbTer.isSelected())		dias += "T";
		if(cbQua.isSelected())		dias += "Q";
		if(cbQui.isSelected())		dias += "U";
		if(cbSex.isSelected())		dias += "E";
		if(cbSab.isSelected())		dias += "A";
		if(cbDom.isSelected())		dias += "D";
		hor.setDias(dias);
		hor.setUsuario(Usuario.getUsuario(GlobalDeclarations.Is_Usuario));
		hor.setDtOpe(new Date(System.currentTimeMillis()));
		return hor;
	}

	@Override
	public JButton getBtNovo() {
		return btNovo;
	}

	@Override
	public JButton getBtExcluir() {
		return btExcluir;
	}

	@Override
	public JButton getBtListar() {
		return btListar;
	}
}
